/*!
 * Copyright 2019 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import {customer} from "@mall/api-services";
import * as _ from "lodash";
import * as React from "react";
import {RouteComponentProps, withRouter} from "react-router";
import Button from "../components/button";
import Flex from "../components/flex";
import Form, {FormObject} from "../components/form";
import NavBar from "../components/navbar";
import Icon from "../icon";

import "./shipping-address-edit.scss";
import ShippingAddressForm from "./shipping-address-form";

interface DeliveryAddressEditProps extends RouteComponentProps {

}

interface DeliveryAddressEditState {

}

class ShippingAddressEdit extends React.Component<DeliveryAddressEditProps, DeliveryAddressEditState> {

    private form: FormObject = Form.createForm('edit-d-a');

    public constructor(props: Readonly<DeliveryAddressEditProps>) {
        super(props);
        this.state = {};
        this.handleSaveDeliveryAddress = this.handleSaveDeliveryAddress.bind(this);
        this.handleDeleteDeliveryAddress = this.handleDeleteDeliveryAddress.bind(this);
    }

    public componentDidMount(): void {
        this.refreshDeliveryAddress();
    }

    public render() {
        const {history} = this.props;
        return (
            <div className="delivery-address-edit">
                <NavBar left={<Icon onClick={() => history.goBack()}
                                    type="left"/>}
                        right={<Button
                            onClick={this.handleSaveDeliveryAddress}
                            type="link">保存</Button>}>编辑收货地址</NavBar>
                <ShippingAddressForm form={this.form}/>
                <Flex className="delivery-address-edit-delete-button"
                      onClick={this.handleDeleteDeliveryAddress}>
                    删除收货地址
                </Flex>
            </div>
        );
    }

    private readonly refreshDeliveryAddress = () => {
        const {match} = this.props;
        const customerId = match.params['customer_id'];
        const deliveryAddressId = match.params['delivery_address_id'];
        customer.CustomerAddressServiceV1.getAddress(customerId, deliveryAddressId)
            .then(deliveryAddress => this.form.setFieldsValue(deliveryAddress));
    };

    private readonly handleSaveDeliveryAddress = () => {
        const {match, history} = this.props;
        const customerId = match.params['customer_id'];
        const deliveryAddressId = match.params['delivery_address_id'];
        const deliveryAddress =
            _.assign(new customer.CustomerAddress(),
                this.form.getFieldsValue(), {
                    id: deliveryAddressId
                });
        customer.CustomerAddressServiceV1
            .updateAddress(customerId, deliveryAddress)
            .then(() => history.goBack());
    };

    private readonly handleDeleteDeliveryAddress = () => {
        const {match, history} = this.props;
        const customerId = match.params['customer_id'];
        const deliveryAddressId = match.params['delivery_address_id'];
        customer.CustomerAddressServiceV1
            .removeAddress(customerId, deliveryAddressId)
            .then(() => history.goBack());
    };
}

export default withRouter(ShippingAddressEdit);
